package com.vf.admin.client.dao;

import com.vf.admin.common.Constants;
import com.vf.admin.common.db.BaseDao;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.entity.AppOrder;
import com.vf.admin.utils.StringEx;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 订单
 */
@Repository
public class AppOrderDao extends BaseDao<AppOrder> {

    /**
     * 查询列表
     */
    public List<AppOrder> list(Map<String,Object> paraMap, SplitPageBean splitPage) {
        ArrayList<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select * from app_order where 1=1  ");
        sbStr.append(" order by createtime desc");
        String sql = sbStr.toString();
        return this.queryList(sql,paramList, AppOrder.class,splitPage);
    }

    /**
     * 详情
     */
    public AppOrder get(Integer id) {
        if (StringEx.isNullOrLing(id)) {
            return null;
        }
        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select * from app_order where id = ?  ");
        paramList.add(id);
        String sql = sbStr.toString();
        return this.queryObj(sql, paramList, AppOrder.class);
    }

    /**
     * 添加/修改
     */
    public void save(AppOrder haoYou) {
        if (haoYou.getId() != null && haoYou.getId() > 0) {
            this.update(haoYou);
        } else {
            haoYou.setId(this.add(haoYou));
        }
    }

    /**
     * 更新发送人头像
     * @param userid
     * @param pic 头像地址
     */
    public void updateImg(Integer userid,String pic){
        if(StringEx.isNull(userid)){
            return ;
        }
        if(StringEx.isNull(pic)){
            return;
        }
        String sql = " UPDATE app_order SET pic = ?  WHERE userid = ? ";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(pic);
        paramList.add(userid);
        this.update(sql,paramList);
    }

    /**
     * 更新发送人昵称
     * @param userid
     * @param nick 昵称
     */
    public void updateNick(Integer userid,String nick){
        if(StringEx.isNullOrLing(userid)){
            return ;
        }
        if(StringEx.isNull(nick)){
            return;
        }
        String sql = " UPDATE app_order SET nick = ?  WHERE userid = ? ";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(nick);
        paramList.add(userid);
        this.update(sql,paramList);
    }

    /**
     * 根据自定义唯一订单号查询代付款的订单信息
     */
    public AppOrder getByOrdernum(String ordernum,Integer type,Integer state) {
        if (StringEx.isNull(ordernum)) {
            return null;
        }
        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select * from app_order where ordernum = ? ");
        paramList.add(ordernum);
        sbStr.append(" and type = ? ");
        paramList.add(type);
        sbStr.append(" and status = ? ");
        paramList.add(state);
        String sql = sbStr.toString();
        return this.queryObj(sql, paramList, AppOrder.class);
    }

    /**
     * 根据唯一订单号和订单ID更新订单状态为已支付
     * @param ordernum   唯一订单号
     * @param type              订单ID
     */
    public void updateStastusYzf(String ordernum,Integer type){
        if(StringEx.isNullOrLing(ordernum)){
            return ;
        }
        String sql = " UPDATE app_order SET status = 2  WHERE  ordernum = ? and type =? ";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(ordernum);
        paramList.add(type);
        this.update(sql,paramList);
    }

    /**
     * 根据唯一订单号和订单ID更新订单回调支付金额
     * @param huidiaomoney   回调金额
     * @param id              订单ID
     */
    public void updateHuiDiaoJine(BigDecimal huidiaomoney, String out_trade_no, Integer id){
        if(StringEx.isNullOrLing(huidiaomoney)){
            return ;
        }
        if(StringEx.isNullOrLing(id)){
            return ;
        }
        String sql = " UPDATE app_order SET huidiaomoney = ?  WHERE id = ? and ordernum = ?";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(huidiaomoney);
        paramList.add(id);
        paramList.add(out_trade_no);
        this.update(sql,paramList);
    }

    public AppOrder isFirstRecharge(Integer userId) {
        List<Object> paramList = new ArrayList<>();
        StringBuilder sbStr = new StringBuilder("select * from app_order where userid = ? and status = 2 limit 1");
        paramList.add(userId);
        String sql = sbStr.toString();
        return this.queryObj(sql, paramList, AppOrder.class);
    }


}
